
var key_type_enum = {
    none: 0,
    s32: 1,
    s64: 2,
    float: 3,
    bool: 4,
    json: 5,
    string: 6
}

var type_default_value = {
    none: 0,
    s32: 0,
    s64: 0,
    float: 0,
    bool: false,
    json: null,
    string: ""
}

function getSplitResult(key)
{
    var strs = [];
    strs = key.split(':');
    if(strs.length > 3)
    {
        for(var i=3; i<strs.length; i++)
        {
            strs[2] +=  ":";
            strs[2] += strs[i];
        }
    }
    return strs;
}

function get_key_type_default(key, err) {
    //var strs = [];
    var strs = getSplitResult(key)//key.split(':');
    //检查格式 a:b or a:b:c
    if (strs.length < 2) {
        err.msg = "key:" + key + " 格式不对,缺少冒号";
        return 0;
    }

    //检查类型是否合法
    var type = strs[1];
    if (typeof key_type_enum[type] != "number") {
        err.msg = "key:" + key + " 无效的类型:" + type;
        return 0;
    }

    //没有默认值的添加默认值
    if (strs.length == 2)
        strs.push(type_default_value[type])
    else //有默认值,检查默认值是否合法
    {
        switch (strs[1]) {
            case 's32':
            case 's64':
                var dst = parseInt(strs[2]);
                if (String(dst) !== strs[2]) {
                    err.msg = "key:" + key + "-无效的默认值";
                    return 0;
                }
                strs[2] = dst;
                break;
            case 'float':
                var dst = parseFloat(strs[2]);
                if (String(dst) !== strs[2]) {
                    err.msg = "key:" + key + "-无效的默认值";
                    return 0;
                }
                strs[2] = dst;
                break;
            case 'bool':
                switch (strs[2]) {
                    case "1":
                        strs[2] = 'true';
                        break;
                    case "0":
                        strs[2] = 'false';
                        break;
                    default:
                        err.msg = "key:" + key + "-无效的默认值";
                        return 0;
                }
                break;
            case 'json':
                if (strs[2] != "null") {
                    try{
                        //alert(strs[2]);
                        JSON.parse(strs[2]);
                    }
                    catch(e)
                    {
                        err.msg = "key:" + key + "-无效的默认值";
                        return 0;
                    }

                }
                else
                strs[2] = "null";
                //alert(strs);
                break;
            case 'string':
                break;
            default:
                err.msg = "意外的类型";
                return 0;
        }
    }

    // console.log(strs);
    return strs;
}

function check_content_legal(content, key, type, default_value, err) {
    //console.log(key + content+ typeof(content));
    if (typeof content == "undefined")
    {
        // alert(default_value);
        return default_value;
    }
    else {
        switch (type) {
            case "s32":
            case "s64":
                var dst = parseInt(content);
                if (String(dst) !== content) {
                    err.msg = "key:" + key + " 无效的内容:" + content;
                    return undefined;
                }
                return dst;
                break;
            case "float":
                var dst = parseFloat(content);
                if (String(dst) !== content) {
                    err.msg = "key:" + key + " 无效的内容:" + content;
                    return undefined;
                }
                return dst;
                break;
            case "bool":
                switch (content) {
                    case "1":
                        return "true";
                        break;
                    case "0":
                        return "false";
                        break;
                    default:
                        err.msg = "key:" + key + " 无效的内容:" + content;
                        return undefined;
                }
                break;
            case "json":
                if (content == "null")
                    return "null";
                try{
                JSON.parse(content);
                }
                catch(e){
                    err.msg = "key:" + key + " json格式错误:" + content;
                    return undefined;
                }
                
                return content;
                break;
            case "string":
                return content;
                break;
            default:
                err.msg = "意外的类型:"+type;
                return undefined;
        }
    }

}

var cf = {

    format: function (json_content, err) {
        var keyIdCount = 0;
        var lineKeyCount = 0;
        if (json_content.length == 0) {
            err.msg = '配置表内容是空的';
            return false;
        }

        //检查#%唯一性,剔除非第一列key的#, 如果key为空,则删除该列
        var key_row_data = json_content[0];
        if (key_row_data.length == 0) {
            err.msg = "没有key";
            return false;
        }

        var key_type_value_sets = [];

        if (key_row_data[0].indexOf("#%") != 0) {
            err.msg = "key不是#%开头";
            return false;
        }


        var wait_delete_culume = [];
        for (var i = 1; i < key_row_data.length; i++) {
            var key_str = key_row_data[i];

            if (key_str && key_str.indexOf('#%') >= 0) {
                err.msg = "多个#%key";
                return false;
            }

            var reg = new RegExp('#', 'g');
            if (key_str) {
                key_str = key_str.replace(reg, '');
                key_row_data[i] = key_str;
            }


            if (!key_str || key_str.length == 0) {
                wait_delete_culume.push(i);
            }
        }

        for (var j = json_content.length - 1; j >= 0; j--) {
            var row_data = json_content[j];
            if (j > 0) {
                var row_data_first_value = row_data[0];
                // alert(row_data_first_value);
                if (row_data_first_value && row_data_first_value.indexOf("#") == 0) {
                    if (row_data_first_value.indexOf("#?") == 0) {
                        row_data[0] = row_data_first_value.replace('?', '');
                    } else {
                        json_content.splice(j, 1);
                    }
                }
            }
            for (var i = wait_delete_culume.length - 1; i >= 0; i--) {

                var delete_culume = wait_delete_culume[i];
                row_data.splice(delete_culume, 1);
            }
        }

        key_row_data = json_content[0];
        var key_map = {};
        for (var i = 0; i < key_row_data.length; i++) {
            var key = key_row_data[i];
            if (key && key_map[key]) {
                err.msg = "重复key:" + key;
                return false;
            }
            if (key)
                key_map[key] = 1;
        }

        //检测key的有效性
        for (var i = 0; i < key_row_data.length; i++) {
            var key_str = key_row_data[i];
            if (key_str.indexOf("[END]") < 0) {
                var ktd = get_key_type_default(key_str, err);
                if (ktd == 0)
                    return false;
                key_type_value_sets.push(ktd);
            }
        }

        //遍历内容,进行类型检查和默认值替换
        for (var i = 1; i < json_content.length; i++) {
            var row_data = json_content[i];
            var row_data_first_value = row_data[0];
            if (row_data_first_value && row_data_first_value.indexOf("#") == 0) {
                if (row_data_first_value.indexOf("#") == 0)
                    continue;
            }

            //console.log("row_data:"+row_data);
            for (var j = 0; j < key_type_value_sets.length; j++) {
                var content = row_data[j];
                var ktd = key_type_value_sets[j];

                var convert_data = check_content_legal(content, ktd[0], ktd[1], ktd[2], err);
                if (convert_data == undefined)
                    return false;

                row_data[j] = convert_data;
            }
        }

        //去掉默认值
        for (var i = 0; i < key_row_data.length; i++) {
            var key_str = key_row_data[i];
            if (key_str.indexOf("[END]") < 0) {
                var strs = [];
                strs = key_str.split(':');
                key_row_data[i] = strs[0] +":"+ strs[1];
            }
        }
        
        // console.log(json_content);
        return true;
    }

}

export default cf;